<?php

namespace app\user\controller;

class Article extends base
{
//前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
        'addclass' => ['only' => 'edit'],
    ];

    /**
     * 栏目
     */
    protected function addclass()
    {
        $this->datas['lang'] = cookie('think_var');
        $this->datas['status']='1';
        $company= model('company')->where($this->datas)->select();
        $this->assign('companyclass', $company);
    }

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();

            $this->datas = array();

            if (isset($data['status']) || !empty($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }

            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
        $this->datas ['contributor'] = $this->getLoginUser()->id;
    }


    public function edit()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['status'] = 0;
            $data['contributor'] = $this->getLoginUser()->id;
            $data['contribute'] = '1';
            $data['uname'] = $this->getLoginUser()->name;
            if(isset($data['source'],$data['review'])){
                $obj['source'] =  $data['source']?$data['source']:'';
                $obj['review'] =  $data['review']?$data['review']:'';
                $data['wechatkey'] = json_encode($obj);
            }

            if (!empty($data['id'])) {
                return $this->update($data);
            }
            $data['lang']=cookie('think_var');
            $result = $this->model->allowField(true)->save($data);
//            $result = db('Article')->insert($data);
            if ($result) {
                $this->success('投稿成功等待审核');
                return ajax_info('投稿成功等待审核', 'y');
            }
            $this->error('投稿失败！');
            return ajax_info('投稿失败！');

        }
//        $wechatkey = model('Article')->where('id',  $this->request->param('id'))->value('wechatkey');
//        $wechatkey = json_decode($wechatkey);
        $data = $this->model->where('id', $this->request->param('id'))
            ->where('contributor', $this->getLoginUser()->id)
            ->find();
        $wechatkey = json_decode($data['wechatkey']);
        $this->assign([
            'data' => $data,
            'wechatkey'=>$wechatkey
        ]);
        return $this->fetch();
    }

    /**
     * 留言
     */
    public function listmass(){
        $list = db('articlemass')
            ->where('uid', $this->getLoginUser()->id)->paginate()
            ->appends($this->request->param())->each(function($item, $key){
                $item['title'] = db('article')->where('id',$item['aid'])->value('title');
                return $item;
            });

        $this->assign([
            'list' => $list,
            'num' => db('articlemass')->where('uid', $this->getLoginUser()->id)->count(),
        ]);

        return $this->fetch();
    }

}
